package person.bing.fbcloud.leetcode;

import java.util.Stack;

/**
 * @author: mengfanbing
 * @date: 2020/10/19 9:36
 */
public class Q844 {
    public static void main(String[] args) {
        String S = "xywrrmp";
        String T = "xywrrmu#p";
        boolean b = backspaceCompare(S, T);
        System.out.println(b);
    }
    private static final char TUI = '#';
    public static boolean backspaceCompare(String S, String T) {
        if (S == T){
            return true;
        }
        if (S.equals(T)){
            return true;
        }
        char[] sChars = S.toCharArray();
        char[] tChars = T.toCharArray();
        int sLength = sChars.length;
        int tLength = tChars.length;
        int longer = sLength > tLength ? sLength : tLength;
        Stack sStack = new Stack();
        Stack tStack = new Stack();
        for (int i=0;i<longer;i++){
            if (i<sLength){
                if (sChars[i] == TUI){
                    if (sStack.isEmpty()){
                    }else {
                        sStack.pop();
                    }
                }else {
                    sStack.push(sChars[i]);
                }
            }
            if (i<tLength){
                if (tChars[i] == TUI){
                    if (tStack.isEmpty()){
                    }else {
                        tStack.pop();
                    }
                }else {
                    tStack.push(tChars[i]);
                }
            }
        }
        return sStack.toString().equals(tStack.toString());
    }
}
